package haoyou.csnf.manager.module.pm.controller.admin.rulesliving;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.CommonResult;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;
import static haoyou.csnf.manager.framework.common.pojo.CommonResult.success;

import haoyou.csnf.manager.framework.excel.core.util.ExcelUtils;

import haoyou.csnf.manager.framework.apilog.core.annotation.ApiAccessLog;
import static haoyou.csnf.manager.framework.apilog.core.enums.OperateTypeEnum.*;

import haoyou.csnf.manager.module.pm.controller.admin.rulesliving.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.rulesliving.RulesLivingDO;
import haoyou.csnf.manager.module.pm.service.rulesliving.RulesLivingService;

@Tag(name = "管理后台 - 生活补贴规则")
@RestController
@RequestMapping("/pm/rules-living")
@Validated
public class RulesLivingController {

    @Resource
    private RulesLivingService rulesLivingService;

    @PostMapping("/create")
    @Operation(summary = "创建生活补贴规则")
    @PreAuthorize("@ss.hasPermission('pm:rules-living:create')")
    public CommonResult<Integer> createRulesLiving(@Valid @RequestBody RulesLivingSaveReqVO createReqVO) {
        return success(rulesLivingService.createRulesLiving(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生活补贴规则")
    @PreAuthorize("@ss.hasPermission('pm:rules-living:update')")
    public CommonResult<Boolean> updateRulesLiving(@Valid @RequestBody RulesLivingSaveReqVO updateReqVO) {
        rulesLivingService.updateRulesLiving(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生活补贴规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:rules-living:delete')")
    public CommonResult<Boolean> deleteRulesLiving(@RequestParam("id") Integer id) {
        rulesLivingService.deleteRulesLiving(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除生活补贴规则")
                @PreAuthorize("@ss.hasPermission('pm:rules-living:delete')")
    public CommonResult<Boolean> deleteRulesLivingList(@RequestParam("ids") List<Integer> ids) {
        rulesLivingService.deleteRulesLivingListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生活补贴规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pm:rules-living:query')")
    public CommonResult<RulesLivingRespVO> getRulesLiving(@RequestParam("id") Integer id) {
        RulesLivingDO rulesLiving = rulesLivingService.getRulesLiving(id);
        return success(BeanUtils.toBean(rulesLiving, RulesLivingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生活补贴规则分页")
    @PreAuthorize("@ss.hasPermission('pm:rules-living:query')")
    public CommonResult<PageResult<RulesLivingRespVO>> getRulesLivingPage(@Valid RulesLivingPageReqVO pageReqVO) {
        PageResult<RulesLivingDO> pageResult = rulesLivingService.getRulesLivingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RulesLivingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生活补贴规则 Excel")
    @PreAuthorize("@ss.hasPermission('pm:rules-living:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRulesLivingExcel(@Valid RulesLivingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RulesLivingDO> list = rulesLivingService.getRulesLivingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生活补贴规则.xls", "数据", RulesLivingRespVO.class,
                        BeanUtils.toBean(list, RulesLivingRespVO.class));
    }

}